﻿@{
    Layout = "";
}
@model Nop.Plugin.DiscountRules.HasOneProduct.Models.RequirementModel
@using Nop.Web.Framework;

<script type="text/javascript">
    $(document).ready(function () {
        $('#saveHasOneProductrequirement@(Model.RequirementId)').click(function () {
            var productIds = $("#@Html.FieldIdFor(model => model.Products)").val();
            var discountId = @Model.DiscountId;
            var requirementId = @Model.RequirementId;
                
            var postData = {
                discountId: discountId, 
                discountRequirementId: requirementId,
                productIds: productIds
            };
            addAntiForgeryToken(postData);

            $.ajax({
                cache:false,
                type: "POST",
                url: "@(Url.Action("Configure", "DiscountRulesHasOneProduct"))",
                data: postData,
                success: function (data) {
                    $('#pnl-save-requirement-result@(Model.RequirementId)').fadeIn("slow").delay(1000).fadeOut("slow");
                    //notify parent if it's a new requirement
                    @if (Model.RequirementId == 0)
                    {
                        <text>$("#discountRequirementContainer").trigger('nopnewdiscountruleadded', [data.NewRequirementId]);</text>
                    }
                },
                error:function (xhr, ajaxOptions, thrownError){
                    alert('@Html.Raw(HttpUtility.JavaScriptStringEncode(T("Admin.Promotions.Discounts.Requirements.FailedToSave").Text))');
                }  
            });
        });
    });
</script>

<div class="form-group">
    <div class="col-md-3">
        @Html.NopLabelFor(model => model.Products)
    </div>
    <div class="col-md-9">
        <div class="input-group">
            @Html.NopEditorFor(model => model.Products)
            <div class="input-group-btn">
                <input type="button" id="saveHasOneProductrequirement@(Model.RequirementId)" class="btn btn-info" value="@T("Admin.Common.Save")" />
            </div>
        </div>
    </div>
</div>
<div class="form-group">
    <div class="col-md-3">
        &nbsp;
        <div id="pnl-save-requirement-result@(Model.RequirementId)" style="display: none; margin-top: 10px;" class="text-green pull-right">
            @T("Admin.Promotions.Discounts.Requirements.Saved")
        </div>
        <div id="discountrequirement-products-check-progress@(Model.RequirementId)" class="please-wait pull-right" style="display: none;">
            @T("Common.Wait...")
        </div>
    </div>
    <div class="col-md-9">
        <input type="submit"
               id="btnAddNewDiscountRequirementProduct@(Model.RequirementId)"
               value="@T("Plugins.DiscountRules.HasOneProduct.Fields.Products.AddNew")"
               onclick="javascript:OpenWindow('@(Url.RouteUrl("Plugin.DiscountRules.HasOneProduct.ProductAddPopup", new {btnId = "btnRefreshDiscountRequirementProducts" + Model.RequirementId, productIdsInput = Html.FieldIdFor(model => model.Products)}))', 800, 850, true); return false;"
               class="btn btn-primary"/>
        <input type="submit" id="btnRefreshDiscountRequirementProducts@(Model.RequirementId)" style="display: none"/>
    </div>
</div>
<div class="form-group">
    <div class="col-md-9 col-sm-offset-3">
        <p id="discountrequirement-product-names@(Model.RequirementId)"></p>
    </div>
</div>


<script type="text/javascript">
    $(document).ready(function() {
        $('#btnRefreshDiscountRequirementProducts@(Model.RequirementId)').click(function() {
            //refresh product list
            loadDiscountRequirementProductFriendlyNames@(Model.RequirementId)();

            //return false to don't reload a page
            return false;
        });
    });

    $(document).ready(function() {
        loadDiscountRequirementProductFriendlyNames@(Model.RequirementId)();
    });
    $(document).ready(function() {
        $('#@Html.FieldIdFor(model => model.Products)')
            .data('timeout', null)
            .keyup(function() {
                clearTimeout($(this).data('timeout'));
                //one second timeout
                $(this).data('timeout', setTimeout(loadDiscountRequirementProductFriendlyNames@(Model.RequirementId), 1000));
            });
    });

    function loadDiscountRequirementProductFriendlyNames@(Model.RequirementId)() {
        var inputValue = $('#@Html.FieldIdFor(model => model.Products)').val();
        if (inputValue) {
            //load friendly names
            $('#discountrequirement-products-check-progress@(Model.RequirementId)').show();

            var postData = {
                productIds: inputValue
            };
            addAntiForgeryToken(postData);

            $.ajax({
                cache: false,
                type: 'POST',
                url: '@Url.RouteUrl("Plugin.DiscountRules.HasOneProduct.LoadProductFriendlyNames")',
                data: postData,
                dataType: 'json',
                success: function(data) {
                    $('#discountrequirement-products-check-progress@(Model.RequirementId)').hide();
                    $('#discountrequirement-product-names@(Model.RequirementId)').text(data.Text);
                },
                failure: function() {
                    $('#discountrequirement-products-check-progress@(Model.RequirementId)').hide();
                    $('#discountrequirement-product-names@(Model.RequirementId)').text('');
                }
            });
        } else {
            $('#discountrequirement-product-names@(Model.RequirementId)').text('');
        }
    }
</script>